package com.lc.cehui.ui.activity;

import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.BusinessIndexActivity;
import com.lc.cehui.http.request.CollectAddApi;
import com.lc.cehui.http.request.CollectDelApi;
import com.lc.cehui.http.request.GoodsXQ;
import com.lc.cehui.http.response.BaseBean;
import com.lc.cehui.http.response.GoodsDetailsBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.GoodesBannerAdapter;
import com.lc.cehui.ui.adapter.RecommendGoodsAdapter;
import com.lc.cehui.util.HtmlUtils;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.ShoppingDialog;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.OnClick;

public class GoodsDetailsActivity extends AppActivity {

    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money_old)
    TextView tvMoneyOld;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_zp)
    TextView tvZp;
    @BindView(R.id.tv_my)
    TextView tvMy;
    @BindView(R.id.tv_th)
    TextView tvTh;
    @BindView(R.id.iv_shop_logo)
    ImageView ivShopLogo;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.recyclerView_type)
    RecyclerView recyclerViewType;
    @BindView(R.id.bt_car)
    Button btCar;
    @BindView(R.id.tv_submit)
    Button tvSubmit;
    @BindView(R.id.ll_comment)
    LinearLayout llComment;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.iv_comment_logo)
    ShapeableImageView ivCommentLogo;
    @BindView(R.id.tv_comment_name)
    TextView tvCommentName;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.ll_tj)
    LinearLayout llTj;
    @BindView(R.id.recyclerView_recommend)
    RecyclerView recyclerViewRecommend;
    @BindView(R.id.tv_comment_body)
    TextView tvCommentBody;
    @BindView(R.id.tv_comment_attr)
    TextView tvCommentAttr;
    @BindView(R.id.ll_goods_ms)
    LinearLayout llGoodsMs;
    @BindView(R.id.img_shouchang)
    CheckBox checkbox;
    @BindView(R.id.rl_business_index)
    RelativeLayout rlBusinessIndex;

    private GoodsDetailsBean.DataBean dataBean;
    private RecommendGoodsAdapter recommendGoodsAdapter;
    private String id;
    private String store_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_details;
    }

    @Override
    protected void initView() {
        recommendGoodsAdapter = new RecommendGoodsAdapter(R.layout.item_recommend_goods, null);
        recyclerViewRecommend.setLayoutManager(new GridLayoutManager(GoodsDetailsActivity.this, 2));
        recyclerViewRecommend.setAdapter(recommendGoodsAdapter);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    collect_add();
                } else {
                    collect_del();
                }
            }
        });
    }

    @Override
    protected void initData() {
        EasyHttp.post(this)
                .api(new GoodsXQ().setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                        .setId(getIntent().getIntExtra("id", 1)))
                .request(new HttpCallback<GoodsDetailsBean>(this) {
                    @Override
                    public void onSucceed(GoodsDetailsBean data) {
                        LogUtils.e(JSON.toJSONString(data));
                        if (callBackCode(data)) {
                            dataBean = data.getData();
                            store_id = data.getData().getStore_id() + "";
                            tvName.setText(dataBean.getName());
                            tvMoney.setText("￥" + dataBean.getPrice());
                            tvMoneyOld.setText("￥" + dataBean.getY_price());
                            switch (dataBean.getMiaoshu().size()) {
                                case 1:
                                    tvZp.setText(dataBean.getMiaoshu().get(0));
                                    break;
                                case 2:
                                    tvZp.setText(dataBean.getMiaoshu().get(0));
                                    tvMy.setText(data.getData().getMiaoshu().get(1));
                                    break;
                                case 3:
                                    tvZp.setText(dataBean.getMiaoshu().get(0));
                                    tvMy.setText(dataBean.getMiaoshu().get(1));
                                    tvTh.setText(dataBean.getMiaoshu().get(2));
                                    break;
                            }
                            id = data.getData().getId() + "";
                            if (data.getData().getIs_collect() == 1) {
                                checkbox.setChecked(false);
                            } else {
                                checkbox.setChecked(true);
                            }
                            if (data.getData().getBanner().size() != 0) {
                                banner.setAdapter(new GoodesBannerAdapter(getContext(), data.getData().getBanner())).setLoopTime(5000).setIndicatorGravity(IndicatorConfig.Direction.CENTER);
                            }

                            if (data.getData().getText().length() != 0) {
                                llGoodsMs.setVisibility(View.VISIBLE);
                                String html = HtmlUtils.setHtml(data.getData().getText());
                                webView.loadData(html, "text/html; charset=UTF-8", null);
                            }
                            if (data.getData().getComment().size() != 0) {
                                llComment.setVisibility(View.VISIBLE);
                                GlideApp.with(GoodsDetailsActivity.this).load(data.getData().getComment().get(0).getAvatar()).into(ivCommentLogo);
                                tvCommentName.setText(data.getData().getComment().get(0).getNickname());
                                tvCommentBody.setText(data.getData().getComment().get(0).getText());
                                tvCommentAttr.setText(data.getData().getComment().get(0).getAttr());
                            }

                            if (data.getData().getAddress() != null) {
                                etAddress.setText(data.getData().getAddress());
                            }

                            if (data.getData().getStore() != null) {
                                tvShopName.setText(data.getData().getStore().getName());
                                GlideApp.with(GoodsDetailsActivity.this).load(data.getData().getStore().getPicurl()).into(ivShopLogo);
                            }

                            if (data.getData().getRecommend().size() != 0) {
                                llTj.setVisibility(View.VISIBLE);
                                recommendGoodsAdapter.setNewInstance(data.getData().getRecommend());
                            }


                        }
                    }
                });
    }

    private void collect_del() {
        EasyHttp.post(this)
                .api(new CollectDelApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(1).setArticle_id(id).setStore_id("").setType_pid("").setType_id(""))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {

                        if (callBackCode(data)) {

                            toast("已取消收藏");
                        }
                    }
                });
    }

    private void collect_add() {
        EasyHttp.post(this)
                .api(new CollectAddApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(1).setArticle_id(id).setStore_id("").setType_pid("").setType_id(""))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {
                        if (data.code == 200) {

                        }
                    }
                });

    }

    @OnClick({R.id.bt_car, R.id.tv_submit, R.id.tv_ckpl, R.id.img_back, R.id.rl_business_index})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_business_index:
                // startActivity(new Intent(this,BusinessIndexActivity.class).putExtra("store_id",store_id));
                break;
            case R.id.bt_car:
            case R.id.tv_submit:
                ShoppingDialog shoppingDialog = new ShoppingDialog(GoodsDetailsActivity.this);
                shoppingDialog.Buynow(JSON.toJSONString(dataBean));
                break;
            case R.id.tv_ckpl:
                Intent intent = new Intent(GoodsDetailsActivity.this, CommentListActivity.class);
                intent.putExtra("goods_id", dataBean.getId());
                startActivity(intent);
                break;
        }
    }


}
